from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import *
import re,redis,random,string
# Create your views here.
class Zc(APIView):
    def post(self,request):
        name=request.data.get('name')
        pwd=request.data.get('pwd')
        if not re.findall(r'[a-zA-Z]\w{4,14}$',name):
            return Response({'code':400,'msg':'格式不正确'})
        Dr.objects.create(name=name,pwd=pwd)
        return Response({'code':200,'msg':'注册成功'})
class Dr(APIView):
    def post(self,request):
        name = request.data.get('name')
        pwd = request.data.get('pwd')
        try:
            user=Dr.objects.get(name=name)
        except Exception as e:
            print(e)
        return Response({
            'code':200,
            'msg':'登陆成功',
        })

class Sxview(APIView):
    def post(self,request):
        you=request.data.get('you')
        Sx.objects.create(
            you=you
        )
        return Response({'code':200,'msg':'添加油箱成功'})

class Mma(APIView):
    def put(self, request, id):
        pwd = request.data.get('pwd')
        Sx.objects.filter(id=id).update(pwd=pwd)
        return Response({'code': 200, 'msg': "修改成功"})
class Sview(APIView):
    def get(self,request):
        data=Sx.objects.all()
        list=[]
        for i in data:
            list.append({
                'id':i.id,
                'you':i.you,
                'pwd':i.pwd,
                'mobile':i.mobile
            })
        return Response(list)

class Sjview(APIView):
        def put(self, request, id):
            mobile = request.data.get('mobile')
            Sx.objects.filter(id=id).update(mobile=mobile)
            return Response({'code': 200, 'msg': "修改成功"})


class Xqview(APIView):
    def get(self,request):
        data=Imgs.objects.all()
        lst=[]
        for i in data:
            lst.append({
                'id':i.id,
                'img':i.img,
                'name':i.name,
                'bo':i.bo,
                'sj': i.sj
            })
        return Response(lst)